﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSDBM.Code.dao;
using VSDBM.Code.dto;
using VSDBM.Code.functions;

public partial class Visual_CRUD_Organism_AddOrganism : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        verificaUser();
    }
    protected void verificaUser()
    {
        VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
        if (!(usuariologado.acessgroup == 1 || usuariologado.acessgroup == 2))
        {
            HttpContext.Current.Session["exception"] = "You are not authorized!";
            Response.Redirect("~/Visual/Errors/error.aspx");
        }
    }

    protected void btnCheckOrganism_Click(object sender, EventArgs e)
    {
        try
        {
            String Organism = zeseeker.getOrganism(txtReferenceID.Text, "nuccore");
            tbOrganismName.Text = Organism;
            txtReferenceID.Enabled = false;
            btnSave.Visible = true;
        }
        catch (Exception expx)
        {
            
            HttpContext.Current.Session["exception"] = expx.Message;
            Response.Redirect("~/Visual/Errors/error.aspx");
        }
        
        

    }


    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {

        string id = txtReferenceID.Text;
        string organismName = tbOrganismName.Text;
        int idOrganism = 0;

        try
        {
            VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
            List<object> lista = new List<object>();
            lista = zeseeker.getReferenceSequencePOST(id, "nuccore");
            VSDBM.Code.dto.Sequence seq = lista[0] as Sequence;

            seq.iduser_register = usuariologado.id;
            seq.status = 1;
            if (zeseeker.checkOrganism(organismName))
            {
                Exception ex = new Exception("Organism Already Exists");
                HttpContext.Current.Session["exception"] = ex.Message;
                Response.Redirect("../../../Visual/Errors/error.aspx");
            }
            else
            {
                idOrganism = OrganismDAO.setOrganismGetId(organismName);
                Reference reference = new Reference(0, seq, idOrganism, 1);
                seq.idorganism = idOrganism;
                ReferenceDAO.add(reference);
                int refid = ReferenceDAO.getId(reference);
                List<Reference_Feature> featurelist = lista[1] as List<Reference_Feature>;
                foreach (Reference_Feature feature in featurelist)
                {
                    feature.idSeqRef = refid;
                    Reference_FeatureDAO.add(feature);
                }

            }
        }
        catch (Exception)
        {

            throw;
        }

        Response.Redirect("../../../Visual/CRUD/Organism/ListOrganism.aspx");
    }
}